//
//  GYPayMethodSelectView.m
//  GoodLuck
//
//  Created by apple on 2018/4/3.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYPayMethodSelectView.h"
#import "GYPayMethodTableViewCell.h"
#import "GYNetWorkTools.h"
#import "GYPayinfo.h"

@interface GYPayMethodSelectView ()<UIGestureRecognizerDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *payPriceLable;
@property (weak, nonatomic) IBOutlet UIButton *confirmPayBtu;
@property (weak, nonatomic) IBOutlet UIView *payView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 数据源 */
@property (nonatomic, strong) NSArray *payList;

/** 代表支付方式 */
@property (nonatomic, assign) NSInteger currentPayIndex;

/** 支付完成通知URL */
@property (nonatomic, strong) NSString *notifyUrl;


@end
@implementation GYPayMethodSelectView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.confirmPayBtu.layer.cornerRadius = 4;
        self.currentPayIndex = 1;
    self.backgroundColor = [UIColor colorWithWhite:0.4 alpha:0.6];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (IBAction)tapGR:(UITapGestureRecognizer *)sender {
    [self setHidden:YES];
    //这里判断是不是直接购买
    if (self.isOrderPay) {
        self.returnIsPayCallBack();
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.payView]) {
        return NO;
    }
    return YES;
}

- (void)setGoods_price:(NSString *)goods_price {
    self.payPriceLable.text = goods_price;
}
- (IBAction)onClick:(UIButton *)sender {
    
    //1.提交订单
    //有没有选中支付方式
    BOOL isExtence = NO;
    GYPayment *payModel;
    for (GYPayment *model in self.payList) {
        if (model.isSelected) {
            isExtence = YES;
            payModel = model;
            break;
        }
    }
    if (!isExtence) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"您还没有选择支付方式!" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }else {
        //跳转到字符界面  支付
        self.returnPayMethodCallBack(payModel, self.notifyUrl);
    }
}

- (void)setPay_sn:(NSString *)pay_sn {
    _pay_sn = pay_sn;
    
    //进行网络请求
    [EasyLodingView showLodingText:@"正在获取付款信息,请稍等!"];
    [GYNetWorkTools requestPayMethodInfo:pay_sn success:^(NSDictionary *obj) {
        //NSLog(@"obj=========%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            [EasyLodingView hidenLoding];
            NSDictionary *pay_info = [datas objectForKey:@"pay_info"];
            GYPayinfo *payInfo = [[GYPayinfo alloc] initWithDic:pay_info];
            self.notifyUrl = [payInfo.app_pay objectForKey:@"notifyUrl"];
            self.payList = payInfo.payment_list;
            [self.tableView reloadData];
        }else if (code == 400) {
            [EasyTextView showText:@"获取付款方式失败!"];
            [EasyLodingView hidenLoding];
        }
        
    } error:^(NSError *error) {
        [EasyTextView showText:@"获取付款方式失败!"];
        NSLog(@"PayMethodInfoFailed====%@",error);
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.payList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYPayMethodTableViewCell *cell = [GYPayMethodTableViewCell cellWithTableView:tableView];
    cell.payment = self.payList[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GYPayment *ment = self.payList[indexPath.row];
    if (indexPath.row == 0 || indexPath.row == 1) {
        if (!ment.isSelected) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                for (GYPayment *model in self.payList) {
                    if ([model.payment_id isEqualToString:ment.payment_id]) {
                        model.selected  = YES;
                    }else {
                        model.selected = NO;
                    }
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [tableView reloadData];
                });
                
            });
        }
    }else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"暂未开放此支付通道,给您造成的不便,敬请原谅!" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}
@end
